#include "Melee.h"

Melee::Melee()
	: mActive (false)
	, mAttackDelay(0.03f)
	, mRenderActive (false)
	, mRenderDelay (0.5f)
{}

Melee::~Melee()
{
}

void Melee::Load(const char* meleeName)
{
	if(meleeName == "cane")
	{
		mSprite.Add("melee1.png");
		mSprite.Add("melee2.png");
		mSprite.Add("melee3.png");
		mSprite.Add("melee4.png");
	}
	else if(meleeName == "skateboard")
	{
		mSprite.Add("enemymelee1.png");
		mSprite.Add("enemymelee2.png");
		mSprite.Add("enemymelee3.png");
		mSprite.Add("enemymelee4.png");
	}
}

void Melee::Unload()
{
	mSprite.Unload();
}

void Melee::Update(float deltaTime)
{
	if(mActive)
	{
		mAttackDelay -= deltaTime;
		if(mAttackDelay <= 0)
		{
			Kill();
			mAttackDelay = 0.03f;
		}
	}

	if(mRenderActive)
	{
		mSprite.Update(deltaTime);
		mRenderDelay -= deltaTime;
		if(mRenderDelay <= 0)
		{
			KillAnimation();
			mRenderDelay = 0.5f;
		}
	}
}

void Melee::Render(const SVector2& viewOffset)
{
	if(mRenderActive)
	{
		int width = mSprite.GetWidth();
		int height = mSprite.GetHeight();

		SVector2 offset(width * 0.5f, height * 0.5f);
		SVector2 renderPosition = mPosition - offset - viewOffset;

		mSprite.SetPosition(renderPosition);
		mSprite.Render();
	}
	//if(mActive)
		//Graphics_DebugCircle(mPosition, 50.0f, 0x00ff00);
}

void Melee::Attack(const SVector2& pos, bool fLeft)
{
	mPosition = pos;
	mActive = true;
	mRenderActive = true;
	mSprite.Play(3.0f, false);
	mSprite.SetFlipH(fLeft);
}

void Melee::Kill()
{
	mActive = false;
}

void Melee::KillAnimation()
{
	mRenderActive = false;
	mSprite.Stop();
	mSprite.Reset();
}